//
//  CFProxySupport.swift
//  
//
//  Created by tangbl93 on 2021/3/27.
//

import UIKit

// MARK: CFProxySupport
public extension UIApplication {
    
    // load system proxy settings
    // PS: if it is set through a VPN, cannot be detected 
    static var yx_systemProxySettings: /*[CFString: Any]?*/ CFDictionary? {
        // #1 cast to dictionary
        // return CFNetworkCopySystemProxySettings()?.takeUnretainedValue() as? [CFString: Any]
        
        // #2 use cfdictionary
        return CFNetworkCopySystemProxySettings()?.takeUnretainedValue()
    }
    
    // Whether the user use http proxy
    static var yx_proxiesHTTPEnable: Bool {
        guard let proxySettings = yx_systemProxySettings else {
            return false
        }
        // #1 cast to dictionary
        // guard let proxiesHTTPEnable = proxySettings[kCFNetworkProxiesHTTPEnable] as? Bool else {
        //    return false
        // }
        
        // #2 use cfdictionary
        guard let proxiesHTTPEnable = proxySettings[kCFNetworkProxiesHTTPEnable] as? Bool else {
            return false
        }
        
        return proxiesHTTPEnable
    }
}
